#include "../../ui.h"
#include "../../common/queue/user_queue.h"
#include "../../common/image_loader/image_loader.h"

///////////////////////// DEFINES ////////////////////

#define FRAME_PATH        "S:/sd/seesaw"
#define FRAME_WIDTH       368
#define FRAME_HEIGHT      448
// 缓存区一次存放的图片数
#define BUFFER_SIZE       10
// 每秒帧数
#define FRAMES_PERSEC     30

///////////////////// VARIABLES ////////////////////

lv_obj_t * ui_HomePage;

static lv_obj_t * img_obj = NULL;
static lv_img_dsc_t img_dsc;

static Queue_t buffer1; // 缓冲区 1
static Queue_t buffer2; // 缓冲区 2
static int current_buffer = 0; // 当前使用的缓冲区（0: buffer1, 1: buffer2）
static int total_frames = 0; // 总帧数

///////////////////// ANIMATIONS ////////////////////

///////////////////// FUNCTIONS /////////////////////

////////////////// timer callback ///////////////////

static void home_timer_cb(lv_timer_t * timer)
{
    static int current_frame_index = 0; // 当前帧索引
    static int current_buffer = 0;     // 当前使用的缓冲区（0: buffer1, 1: buffer2）

    // 获取当前缓冲区
    Queue_t *active_buffer = (current_buffer == 0) ? &buffer1 : &buffer2;

    // 获取当前帧数据
    uint8_t *frame_data = (uint8_t *)image_loader_get_frame_data(active_buffer);
    if (!frame_data) {
        LV_LOG_ERROR("Failed to get frame data");
        return;
    }

    // 设置 img_dsc 的数据
    img_dsc.data = frame_data;
    lv_img_set_src(img_obj, &img_dsc);

    // 更新帧索引
    current_frame_index++;
    if (current_frame_index >= total_frames) {
        current_frame_index = 0; // 循环播放
    }

    // 检查是否需要加载新的缓冲区
    if (queue_is_empty(active_buffer)) {
        // 切换到下一个缓冲区
        current_buffer = 1 - current_buffer;
        active_buffer = (current_buffer == 0) ? &buffer1 : &buffer2;

        // 加载新的帧数据到下一个缓冲区
        int start_frame = current_frame_index;
        if (!image_loader_load_frames(active_buffer, start_frame)) {
            LV_LOG_ERROR("Failed to load frames starting from %d", start_frame);
        }
    }
}

///////////////////// SCREENS ////////////////////

void ui_HomePage_init(void)
{
    ui_HomePage = lv_obj_create(NULL);
    // test fps
    // lv_demo_benchmark();

    // img
    // 初始化图像加载器
    if (!image_loader_init(FRAME_PATH, FRAME_WIDTH, FRAME_HEIGHT, BUFFER_SIZE)) {
        LV_LOG_ERROR("Failed to initialize image loader");
        return;
    }
    // 获取总帧数
    total_frames = image_loader_get_frame_count();
    if (total_frames == 0) {
        LV_LOG_ERROR("No frames found in directory");
        return;
    }
    // 分配buffer空间
    if (!queue_init(&buffer1, BUFFER_SIZE) || !queue_init(&buffer2, BUFFER_SIZE)) {
        LV_LOG_ERROR("Failed to initialize buffers");
        return;
    }
    // 加载第1个缓冲区
    if (!image_loader_load_frames(&buffer1, 0)) {
        LV_LOG_ERROR("Failed to preload frames");
        return;
    }
    // 加载第2个缓冲区
    if (!image_loader_load_frames(&buffer2, 0 + BUFFER_SIZE)) {
        LV_LOG_ERROR("Failed to preload frames");
        return;
    }

    img_dsc.header.always_zero = 0;
    img_dsc.header.w = FRAME_WIDTH;
    img_dsc.header.h = FRAME_HEIGHT;
    img_dsc.header.cf = LV_IMG_CF_TRUE_COLOR;
    img_dsc.data_size = FRAME_WIDTH * FRAME_HEIGHT * 2;
    img_dsc.data = image_loader_get_frame_data(&buffer1);
    img_obj = lv_img_create(ui_HomePage);
    lv_obj_align(img_obj, LV_ALIGN_CENTER, 0, 0);
    lv_img_set_src(img_obj, &img_dsc);

    // clk
    lv_obj_t* ui_Label3 = lv_label_create(ui_HomePage);
    lv_obj_set_width(ui_Label3, LV_SIZE_CONTENT);   /// 1
    lv_obj_set_height(ui_Label3, LV_SIZE_CONTENT);    /// 1
    lv_obj_set_x(ui_Label3, -1);
    lv_obj_set_y(ui_Label3, -166);
    lv_obj_set_align(ui_Label3, LV_ALIGN_CENTER);
    lv_label_set_text(ui_Label3, "11:59");
    lv_obj_set_style_text_color(ui_Label3, lv_color_hex(0x000000), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_opa(ui_Label3, 255, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_text_font(ui_Label3, &ui_font_simhei64, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_radius(ui_Label3, 10, LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_color(ui_Label3, lv_color_hex(0xFFFFFF), LV_PART_MAIN | LV_STATE_DEFAULT);
    lv_obj_set_style_bg_opa(ui_Label3, 128, LV_PART_MAIN | LV_STATE_DEFAULT);

    // timer
    lv_timer_t * timer = lv_timer_create(home_timer_cb, 1000/FRAMES_PERSEC,  NULL);
}
